/*
	Created: Jonathan Turner
		On : 10/18/2013
Description: Add an employee, creating both their daycare.Employee record as well as 
			 creating them in the asp.net membership tables.
	Updates:
*/

ALTER PROCEDURE daycare.AddEmployee
  @FirstName VARCHAR(100)
, @LastName VARCHAR(100)
, @PhoneNumber VARCHAR(15)
, @EmailAddress VARCHAR(100)
, @IsEmployed BIT
, @ImageId INT
, @UName VARCHAR(100)
, @Pwd VARCHAR(100)
, @Salt VARCHAR(100)
AS 
  BEGIN
    INSERT  INTO daycare.Employees
            (
             FirstName
           , LastName
           , PhoneNumber
           , EmailAddress
           , IsEmployed
           , ImageId
            )
    VALUES  (
             @FirstName
           , @LastName
           , @PhoneNumber
           , @EmailAddress
           , @IsEmployed
           , @ImageId
            )

    DECLARE @EmployeeId INT = SCOPE_IDENTITY();
    DECLARE @EmployeeIdString VARCHAR(10) = (CONVERT(VARCHAR(10),@EmployeeId));
    DECLARE @EmployeeProfileProperty VARCHAR(50) = 'EmployeeId:S:0:' + CONVERT(VARCHAR(10),LEN(@EmployeeId))
    DECLARE @AppName VARCHAR(100) = 'Daycare';
    DECLARE @AppId UNIQUEIDENTIFIER = (
                                       SELECT ApplicationId
                                       FROM   dbo.aspnet_Applications
                                       WHERE  ApplicationName = @AppName
                                      )
    DECLARE @Timestamp DATETIME = GETDATE();
    DECLARE @UserId UNIQUEIDENTIFIER;
  
    EXEC dbo.aspnet_Membership_CreateUser @ApplicationName = @AppName,@UserName = @UName,@Password = @Pwd,@PasswordSalt = @Salt,@Email = @EmailAddress,@PasswordQuestion = '',@PasswordAnswer = '',@IsApproved = 1,@CurrentTimeUtc = @Timestamp,@CreateDate = @Timestamp,@UniqueEmail = 1,@PasswordFormat = 1,@UserId = @UserId
    EXEC dbo.aspnet_Profile_SetProperties @ApplicationName = @AppName,@PropertyNames = @EmployeeProfileProperty,@PropertyValuesString = @EmployeeIdString,@PropertyValuesBinary = '0x',@UserName = @UName,@IsUserAnonymous = 0,@CurrentTimeUtc = @Timestamp

  END
 GO
